package com.mingxingyi.applet.config;

import com.mingxingyi.applet.interceptor.JwtTokenUserInterceptor;
import com.mingxingyi.applet.service.impl.UserInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;


    public void addInterceptors(InterceptorRegistry registry) {
        log.info("注册拦截器成功---");
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/applet/getUserInfoOnFree")
                .excludePathPatterns("/applet/login")
                .excludePathPatterns("/applet/loginFirst")
                .excludePathPatterns("/applet/getPhoneNumberFree")
                .excludePathPatterns("/error");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowCredentials(true);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String resolutionPath = System.getProperty(UserInfoServiceImpl.FILE_PATH_ENV) + UserInfoServiceImpl.FILE_PATH_IMAGES;
        String relativePath = System.getProperty(UserInfoServiceImpl.FILE_RELATIVE_PATH_ENV);
        System.out.println(resolutionPath);
        System.out.println(relativePath);
        registry.addResourceHandler(relativePath)
                .addResourceLocations("file:" + resolutionPath);
    }
}
